import {
	baseUrl
} from './baseUrl.js'
export default {
	// 全局配置
	common: {
		baseUrl, //请求地址
		header :{
			'Content-Type': 'application/x-www-form-urlencoded', //'application/x-www-form-urlencoded'
			"Authorization": 'Bearer '+uni.getStorageSync('token') || '' ,
		},
		data: {
			port:'2'
		},
		method: 'GET',
		dataType: 'json'
	},
	//正在请求的地址
	loadingArr:[],
	// 加载动画
	loadingNmuber:0,
	startLoading(){
		if(this.loadingNmuber==0){
			// uni.showLoading({
			// 	mask:true
			// })
		}
		this.loadingNmuber++;
	},
	endLoading(){
		if(this.loadingNmuber==0) return;
		this.loadingNmuber--;
		if(this.loadingNmuber==0){
			// setTimeout(()=>{
			// 	uni.hideLoading();
			// },500)
		}
	},
	// 请求 返回promise
	request(options = {}) {
		if(options.data.noLoading!=1){
			this.startLoading();
		}
		options.url = this.common.baseUrl + options.url;
		options.header = options.header || this.common.header;
		options.data = options.data || this.common.data;
		options.data.port = '2'
		options.method = options.method || this.common.method;
		options.dataType = options.dataType || this.common.dataType;
		let Authorization = 'Bearer '+uni.getStorageSync('token') || '' ;
		if (Authorization) {
			options.header.Authorization = Authorization;
		}
		if(options.data.isArray==1){
			options.header["Content-Type"] = 'application/json'
		}
		// 请求
		return new Promise((resolve,reject) => {
			// console.log(options,'接口信息');
			uni.request({
				...options,
				success: (result) => {
					if(result.data.code== 200001){
						resolve(result.data);
					}else if(result.data.code== 401001){ 
						uni.showToast({
							title:'请先登录',
							icon:'none'
						})
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/ChooseName/ChooseName'
							})
						},1500)
					}else{
						// resolve(result.data);
						if(result.data.message){
							uni.showToast({
								title:result.data.message,
								icon:'none'
							})
						}
						reject(result.data);
					}
					this.endLoading();
				},
				fail: (error) => {
					uni.showToast({
						title: '网络连接失败',
						icon: 'none'
					});
					this.endLoading();
					reject(error)
				}
			});
		})
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	// options请求
	options(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'options'
		return this.request(options)
	},
	// put请求
	put(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'put'
		return this.request(options)
	},
	// head请求
	head(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'head'
		return this.request(options)
	},
	// trace请求
	trace(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'trace'
		return this.request(options)
	},
	// connect请求
	connect(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'connect'
		return this.request(options)
	},
}